package com.epam.client.controller;

import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

import org.apache.log4j.Logger;

import com.epam.DAO.BillDAO;
import com.epam.DAO.RequestDAO;
import com.epam.DAO.Factories.DAOFactory;
import com.epam.entity.Bill;
import com.epam.entity.Client;
import com.epam.entity.Request;
import com.epam.entity.Status;
import com.epam.utils.JsfUtil;

public class BillsManageBean {

	private static Logger log = Logger.getLogger(BillsManageBean.class);

	private DAOFactory daoFactory;
	private BillDAO billDAO;

	private RequestDAO requestDAO;

	private DataModel<Bill> bills;
	private Bill bill;

	private int page = 1;

	public BillsManageBean() {
		daoFactory = DAOFactory
				.getDAOFactory(DAOFactory.CURRENT_IMPLEMENTATION);
		billDAO = daoFactory.getBillDAO();
		requestDAO = daoFactory.getRequestDAO();
		bills = new ListDataModel<Bill>();
	}

	// Getters
	// ------------------------------------------------------------------------------------

	public int getPage() {
		return page;
	}

	// Setters
	// ------------------------------------------------------------------------------------

	public void setPage(int page) {
		this.page = page;
	}

	public void initBill() {
		bill = bills.getRowData();
	}

	public DataModel<Bill> getBills() {
		Client client = (Client) JsfUtil.getAttribute("client");
		log.debug("BillsManageBean getBills");
		bills.setWrappedData(billDAO.getUserBills(client, Status.NOT_PAYED));
		return bills;
	}

	// public void initBillsList(){
	// Client client = (Client) JsfUtil.getAttribute("client");
	// bills.setWrappedData(billDAO.getUserBills(client, Status.NOT_PAYED));
	// }

	public void payBill() {
		Request request = bill.getRequest();
		request.setStatus(Status.ACTIVE);
		requestDAO.update(request);

		bill.setStatus(Status.PAYED);
		billDAO.update(bill);
	}

}
